﻿@model IEnumerable<MVC4Shop.Models.ProductModel>

@{
    ViewBag.Title = "Index";
}

<script type="text/jscript">
    $(document).ready(function () {
        $('.addToCart').click(function () {
            var id = $(this).attr('id');
            $.ajax({
                url: '@Url.Action("AddToCart", "ShoppingCart")',
                data: JSON.stringify({ product_id: id }),
                type: 'POST',
                contentType: 'application/json; charset=utf-8',
                dataType: 'json',
                success: function (result) {
                    if (result.kq) {
                        alert("Đã thêm vào giỏ hàng");
                    }
                },
                error: function () {
                    alert("Không thể thêm vào giỏ hàng");
                }

            });
        });
    });
</script>

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table>
    <tr>

        <th>
            @Html.DisplayNameFor(model => model.name)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.image)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.price)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.details)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.createdate)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.hit)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.category_id)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.catategory_name)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.color)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.size)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.image)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.price)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.details)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.createdate)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.hit)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.category_id)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.catategory_name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.color)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.size)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.id }) |
            @Html.ActionLink("Details", "Details", new { id=item.id }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.id })
            <input type="button" class="addToCart" value="Mua" id="@item.id"/>
        </td>
    </tr>
}

</table>
